﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BaseSet.Data;
using BaseSet.Logic;
using BaseSet.Entity;
using BaseSet.Framework;
using BaseSet.Entity.Common;
using BaseSet.Entity.Common.Text;
using BaseSet;
using System.Data;

namespace WebSite.Admin.SystemSet
{
    public partial class NewsManager : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindNewsType();
                LoadContent(false);

                // 从BasePage中得到
                PageLiteral.Text = BuildPageLinks();
            }
        }


        #region 加载列表
        private void LoadContent(bool mark)
        {

            //设置控件属性，从BasePage中得到
            PageConfig(NewsUPager);

            LoadCondition();

            string newstype = ddlNewsType.SelectedValue;

            //List<Member> list = new List<Member();
            NewsHelper nhelper = new NewsHelper();
            Criteria c = new Criteria();
            
            if (newstype != "-1")
            {
                c.Add(CriteriaType.Equals, "NewsTypeID", newstype);
            }

            NewsUPager.ItemCount = nhelper.QueryNewsCountBySearch(c);

            // 以下的值从BasePage中得到
            NewsUPager.UrlFormat = UrlFormat;
            NewsUPager.PrefixText = GetPreFixText(NewsUPager);

            if (mark)
            {
                PagerInit(NewsUPager);
            }

            string[] fields = new string[] { "ID", "NewsTypeID", "Title", "NewsContent", "Flag", "CreateDate" };

            DataTable dt = new DataTable();
            dt = nhelper.QueryNewsBySearchToTable(c, new Order[] { new Order("CreateDate", OrderMode.Desc) }, NewsUPager.Begin - 1, NewsUPager.Count, fields);

            dt.Columns.Add(new DataColumn("NewsType"));
            dt.Columns.Add(new DataColumn("strCreateDate"));

            if (dt != null && dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    string newstypeid = dr["NewsTypeID"] + "";
                    NewsType ntypeinfo = new NewsType();
                    NewsTypeHelper ntypehelper = new NewsTypeHelper();
                    ntypeinfo = ntypehelper.GetNewsType(newstypeid);
                    dr["NewsType"] = ntypeinfo.NewsTypeName;

                    DateTime dtCreateDate = Convert.ToDateTime(dr["createdate"]);
                    dr["strCreateDate"] = dtCreateDate.ToString("yyyy-MM-dd");
                }
            }

            rptNews.DataSource = dt;
            rptNews.DataBind();
        }
        #endregion

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            SetCondition();
            LoadContent(true);
        }

        #region 列表事件
        protected void rptNews_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            HiddenField hidID = (HiddenField)e.Item.FindControl("hidID");

            string id = hidID.Value;

            if (e.CommandName == "edit")//编辑
            {
                Response.Redirect("NewsEdit.aspx?id=" + id);
            }
            else if (e.CommandName == "delete")//删除
            {
                News ninfo = new News();
                NewsHelper nhelper = new NewsHelper();

                ninfo = nhelper.GetNews(id);
                ninfo.ID = id;
                ninfo.Flag = 9;

                nhelper.EditNews(ninfo);

                Tools.Alert("删除成功！");
            }

            LoadContent(false);
        }
        #endregion

        #region 查询条件设置
        private void SetCondition()
        {
            string newstype = ddlNewsType.SelectedValue;

            //将查询条件放入Session中
            Session["QueryList"] = newstype + "@" + "NewsManager";

        }

        private void LoadCondition()
        {
            string condition = Session["QueryList"] + "";

            string[] conditionAll = condition.Split('@');
            if (conditionAll.Length == 2)
            {
                string page = conditionAll[1];
                if (page == "NewsManager")
                {
                    ddlNewsType.SelectedValue = conditionAll[0];
                }
            }
        }
        #endregion

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Response.Redirect("NewsAdd.aspx");
        }

        #region 加载类别
        private void BindNewsType()
        {
            NewsTypeHelper nhelper = new NewsTypeHelper();

            List<NewsType> list = nhelper.GetNewsTypes();
            ddlNewsType.DataSource = list;
            ddlNewsType.DataTextField = "NewsTypeName";
            ddlNewsType.DataValueField = "ID";
            ddlNewsType.DataBind();

            ListItem li = new ListItem();
            li.Value = "-1";
            li.Text = "-请选择-";
            ddlNewsType.Items.Insert(0, li);
        }
        #endregion
    }
}